<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/itemall' }">货物管理</el-breadcrumb-item>
      <el-breadcrumb-item>货物列表</el-breadcrumb-item>
      <el-breadcrumb-item>鹰联邦</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card" style="width: 1300px;">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getItemList">
            <el-button slot="append" icon="el-icon-search" @click="getItemList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddItem">添加商品</el-button>
        </el-col>
        <el-col :span="4" style="margin-left: 425px;">
            <span>统计图表&emsp;:&emsp;</span>
            <el-button type="success" icon="el-icon-message" circle  @click="drawer = true"></el-button>
        </el-col>
      </el-row>




      <el-drawer
        title="统计图表"
        :visible.sync="drawer"
        :before-close="handleClose"
        >
        <YLBZZ></YLBZZ>
      </el-drawer>




      <!-- 获取商品列表数据 -->
      <el-table :data="itemList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="title" label="商品标题" width="100px">
        </el-table-column>
        <el-table-column prop="sellPoint" label="卖点信息" width="350px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" label="价格(元)" width="80px">
          <template slot-scope="scope">
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="pricerow" label="原价(元)" width="80px">
          <template slot-scope="scope">
            {{scope.row.pricerow | priceFormat}}
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量" width="80px">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="80px">
          <template slot-scope="scope">
            <!-- change:switch状态变化时的回调函数
                 updateStatus(scope.row):作用域插槽-->
            <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
              @change="updateStatus(scope.row)"></el-switch>
          </template>
        </el-table-column>


        <el-table-column prop="display" label="首页展示" width="80px">
          <template slot-scope="scope">
            <!-- change:switch状态变化时的回调函数
                 updateStatus(scope.row):作用域插槽-->
            <el-switch v-model="scope.row.display" active-color="#13ce66" inactive-color="#ff4949"
              @change="updateItemDisplay(scope.row)"></el-switch>
          </template>
        </el-table-column>


       <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateItemBtn(scope.row)" round>修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteItemBtn(scope.row)" round>删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>

<!-- 定义修改页面 当对话框关闭时,将表格数据重置 -->
<!-- updateDialogVisible:为true时弹框弹出 -->
    <el-dialog title="修改商品信息" :visible.sync="updateDialogVisible" width="50%" @close="closeUpdateDialog">
      <el-form :model="updateUserModel" ref="updateUserRef" label-width="100px">
        <el-form-item label="标题" prop="title">
          <!-- 标题是不可以修改的-->
          <el-input v-model="updateUserModel.title"  disabled ></el-input>
        </el-form-item>
        <el-form-item label="卖点" prop="shellpoint">
          <el-input v-model="updateUserModel.sellPoint"></el-input>
        </el-form-item>
        <el-form-item label="卖价(元)" prop="price">
          <el-input v-model="updateUserModel.price"></el-input>
        </el-form-item>
        <el-form-item label="原价(元)" prop="pricerow">
          <el-input v-model="updateUserModel.pricerow"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="updateUserModel.num"></el-input>
        </el-form-item>
        <el-button type="primary" v-show="false">{{this.i}}</el-button>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible = false" >取 消</el-button>
        <el-button type="primary" @click="updateItem">确 定</el-button>
      </span>
    </el-dialog>



  </div>
</template>

<script>
  import YLBZZ from '../report/ylb_zz.vue'
  export default {
    components:{
      YLBZZ
    },
    data() {
      return {
        drawer: false,
        i:null,
        // direction: 'rtl',
        //1.定义商品列表信息
        itemList: [],
        //2.定义分页对象
        queryItemInfo: {
          query: '', //定义查询参数,与输入的内容进行双向绑定
          pageNum: 1,
          pageSize: 10
        },
        total: 0,
        updateDialogVisible: false,
        //定义修改用户数据封装
        updateUserModel: {
        }
      }
    },
    created() {
      window.eventBus.$on('current',(name) =>{
         this.i = name
       })
    },
     updated() {
       if(this.i !== this.queryItemInfo.id){
         this.queryItemInfo.id = this.i
          this.getItemList()
       }
    },
    methods: {
      getCharts(){
        dialogVisible = true
      },
       handleClose(done) {
              this.$confirm('确认关闭？')
                .then(_ => {
                  done();
                })
                .catch(_ => {});
       },
      //实现商品信息分页查询
      //2.调用this.getItemList()
      async getItemList() {
        const {
          data: result
        } = await this.$http.get("/yingliangbang1/getItemList", {
          params: this.queryItemInfo  //将分页对象传给后台
        })
        if (result.status !== 200) return this.$message.error("商品列表查询失败")
        this.itemList = result.data.rows //获取商品列表信息,将其传给itemList
        this.total = result.data.total   //获取商品列表中数据的总数
      },
      //通过JS格式化时间
      formatDate(row, column, cellValue, index) {
        let date = new Date(cellValue)
        let year = date.getFullYear()
        let month = (date.getMonth() + 1 + '').padStart(2, '0')
        let day = (date.getDate() + '').padStart(2, '0')
        let HH = (date.getHours() + '').padStart(2, '0')
        let MM = (date.getMinutes() + '').padStart(2, '0')
        let SS = (date.getSeconds() + '').padStart(2, '0')
        return year + '-' + month + '-' + day + ' ' + HH + ":" + MM + ":" + SS
      },
      //条数变化时 调用
      handleSizeChange(size) {
        this.queryItemInfo.pageSize = size
        this.getItemList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryItemInfo.pageNum = current
        this.getItemList()
      },
      async updateStatus(item) {
        const {
          data: result
        } = await this.$http.put("/yingliangbang1/updateItemStatus", {
          id: item.id,
          status: item.status
        })
        if (result.status !== 200) return this.$message.error("更新状态失败")
        this.$message.success("更新状态成功")
      },
      async updateItemDisplay(item) {
        const {
          data: result
        } = await this.$http.put("/yingliangbang1/updateItemDisplay", {
          id: item.id,
          display: item.display,
          itemCatId: item.itemCatId
        })
        if (result.status !== 200){
          item.display = false
          return this.$message.error("首页展示已达最大")
        }
        this.$message.success("成功")
      },
      async deleteItemBtn(item) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/yingliangbang1/deleteItemById", {
            params: {
              id: item.id
            }
          })
          if (result.status !== 200) return this.$message.error("商品删除失败")
          this.$message.success("商品删除成功")
          //重新获取商品列表信息
          this.getItemList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //转向到商品新增页面
      toAddItem(){
        this.$router.push("/itemall/addyinglianbang")
      },

      async updateItemBtn(item){
        this.updateDialogVisible = true
        const {data: result} = await this.$http.get("/yingliangbang1/"+item.id)
        if(result.status !== 200) return this.$message.error("用户查询失败")
        this.updateUserModel = result.data
        console.log(result.data)
      },
      /* 关闭弹框时实现表格的重置*/
      closeUpdateDialog(){
        //重置表格数据
        this.$refs.updateUserRef.resetFields()
      },
      //修改商品分类信息
      async updateItem(){
       let item= {}
       item.id=this.updateUserModel.id
       item.title=this.updateUserModel.title
       item.sellPoint=this.updateUserModel.sellPoint
       item.price=this.updateUserModel.price
       item.pricerow=this.updateUserModel.pricerow
       item.num=this.updateUserModel.num
       item.images=this.updateUserModel.images
       const {
         data: result
       } = await this.$http.put('/yingliangbang1/updateItem', item)
       if (result.status !== 200) return this.$message.error("更新商品失败")
       this.$message.success("更新商品成功")
       this.updateDialogVisible = false
       this.getItemList()
      }
     }
  }
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
